var id;
var ttt=1;
var text;

var duankou='0'
var kehu='0'
var pingtai='0'
var CangName=""
var code=""
var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

$.each(warehouses.data,function(k,s){
    code +=s.warehouseCode+ ","
    CangName+=s.warehouseName+"/"

})
//去掉最后一个逗号(如果不需要去掉，就不用写)
if (code.length > 0) {
    code = code.substr(0, code.length - 1);
}
if (CangName.length > 0) {
    CangName = CangName.substr(0, CangName.length - 1);
}

// $("#putong").attr("checked", true);
// $("#qianyue").attr("checked", true);

// 保存按钮
$("#form-member").validate({
    rules: {

    },
    //提交表单数据
    submitHandler:function(form){

        //循环class，获取到每个check的选中状态
        if (duankou=='0'){
            $("#yidong").css('outline','1px solid red')
            $("#pc").css('outline','1px solid red')
            layer.msg("请配置端口", {icon: 6, time: 3000});
            return false
        }
        if (pingtai=='0'){
            $("#putong").css('outline','1px solid red')
            $("#qianyue").css('outline','1px solid red')
            layer.msg("请配置平台", {icon: 6, time: 3000});
            return false
        }
        if(pingtai=='10'||pingtai=='10,20'){
            if (kehu=='0'){
                $("#fen").css('outline','1px solid red')
                $("#nofen").css('outline','1px solid red')
                layer.msg("请配置签约平台下的客户类型", {icon: 6, time: 3000});
                return false
            }
        }
        if (code==''){
            var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

            $.each(warehouses.data,function(k,s){
                var warecode= document.getElementById('yun'+s.warehouseCode+'')
                $(warecode).css('outline','1px solid red')

            })

            layer.msg("请配置区域", {icon: 6, time: 3000});
            return false
        }

        ue.ready(function() {
            text = ue.getContent();
        });

        //提交
        var user_data = {
            port:duankou,
            platform:pingtai,
            warehouse_code:code,
            warehouse_name:CangName,
            different_customer_code:kehu,

            title : $("#title").val()+"_方盛云采",
            started_at : $("#logmin").val(),
            end_in : $("#logmax").val(),
            category_id :getQueryString("category_id"),
            rich_text:text,
            created_name:JSON.parse(web.readCookie("loginData")).user.username,
            created_by:JSON.parse(web.readCookie("loginData")).user.user_ID,
        };
        //获取返回的数据
        var succCallBack = function (data, status, response) {
            console.log(data);
            if(data.errorCode=="0") {
                id=data.id
                edit_id()
            }
            if (data.errorCode=="93"){
                layer.msg('开始时间不能大于结束时间,请重新查看', {icon: 6, time: 3000});
                return
            }
        }
        //给一个接口地址提交数据
        ttt=ttt+1;
        if (ttt>2){
            return false;
        }else{
            return web.doAjax(root.sroreinter.save_activity_conf, 'post', user_data, succCallBack);
        }
    }
})
function edit_id() {
    //如果端口是app,那么加上?equipment=h5

    var isApp=''
    var qianYueUrl
    var urlPathOrdinary
    //https://'+ window.location.host +'/api/admin/#/Activity/411.html?equipment=h5
    //https://'+ window.location.host +'/api/admin/#/Activity?activityId=412&equipment=h5
    if (duankou=='20'){
        //属于移动端
        isApp='&equipment=h5'
        qianYueUrl=root.sroreinter.contractNofront+"Activity?activityId="+id+isApp,
        urlPathOrdinary=root.sroreinter.ordinaryNofront+"Activity?activityId="+id+isApp
    }else{
        qianYueUrl=root.sroreinter.Activitycontract+"Activity/"+id+".html"+isApp,
        urlPathOrdinary=root.sroreinter.ActivityOrdinary+"Activity/"+id+".html"+isApp
    }


    $.ajax({
        type: "post",
        url: root.sroreinter.save_activity_conf,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            activity_id:id,
            //签约平台url地址
            url_path:qianYueUrl,
            //普通平台url地址
            url_path_ordinary:urlPathOrdinary,
            edit_id:1

        },
        //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                console.log(data.data);
                layer.msg('操作成功!', {icon: 6, time: 1000});
                parent.comPageNum = 1;
                parent.queryDataFn(1);
                setTimeout( function () {
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                },700);
            }
        }
    })
}

function changeCang() {
    code=''
    CangName=''
    var warcode=""//id
    var warcodename=""//name
    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data,function(k,s){
            if (document.getElementById('yun' + s.warehouseCode + '').checked == true) {//获取选中的id
                warcode += document.getElementById('yun' + s.warehouseCode + '').value + ","//逗号拼接获取到的所以id
                warcodename += document.getElementById('yun' + s.warehouseCode + '').name + "/"
            }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    if (warcodename.length > 0) {
        warcodename = warcodename.substr(0, warcodename.length - 1);
    }

    code=warcode
    CangName=warcodename
}


function changePingtai() {
    $("#putong").css('outline','')
    $("#qianyue").css('outline','')

    if ($("#qianyue").is(":checked")&&$("#putong").is(":checked")){
        $("#kehuleixing").removeAttr("style");
        pingtai='10'+","+'20'
    }
    if ($("#qianyue").is(":checked")&&!$("#putong").is(":checked")){
        $("#kehuleixing").removeAttr("style");
        pingtai='10'
    }
    if ($("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#kehuleixing").attr("style", "display:none");
        pingtai='20'
    }
    if (!$("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#kehuleixing").attr("style", "display:none");
        pingtai='0'
    }
}
var len
var common=70//要限制输入的字数
$('#commonText').text('('+common+')')
function changeDuankou() {
    $("#yidong").css('outline','')
    $("#pc").css('outline','')
    var title = $("#title").val()

    if ($("#yidong").is(":checked")&&!$("#pc").is(":checked")){
        duankou='20'

        common=70

        title = title.substr(0,common)
        $("#title").val(title)


    }
    if ($("#pc").is(":checked")&&!$("#yidong").is(":checked")){
        duankou='10'

        common=100

        title = title.substr(0,common)
        $("#title").val(title)

    }
    if (!$("#pc").is(":checked")&&!$("#yidong").is(":checked")){
        duankou='0'
    }
    strlen(title)
    $('#commonText').text('('+common+')')
    console.log(duankou)
}

function changeKehu() {
    $("#fen").css('outline','')
    $("#nofen").css('outline','')

    if ($("#fen").is(":checked")&&$("#nofen").is(":checked")){
        kehu='10'+","+'20'
    }
    if ($("#fen").is(":checked")&&!$("#nofen").is(":checked")){
        kehu='10'
    }
    if ($("#nofen").is(":checked")&&!$("#fen").is(":checked")){
        kehu='20'
    }
    if (!$("#nofen").is(":checked")&&!$("#fen").is(":checked")){
        kehu='0'
    }
    console.log(kehu)
}

//限制字符

var zifushu = 0
var Preser

function checkMaxIut(me) {
    var text=$(me).val()
    strlen(text)

    if(len>common){
        text=text.substr(0,zifushu)

        $(me).val(text)
    }else {
        Preser=$(me).val()
    }
}
function strlen(str){
    var cishu=0
    len = 0;
    for (var i=0; i<str.length; i++) {
        var c = str.charCodeAt(i);
        //单字节加1
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
            len++;
        } else {
            len+=2;
        }
        if (len>common){
            if (cishu==0){
                zifushu=i
                cishu=1
            }
        }
    }
    $('#textSpan').text((len>common)?common:len)
    return len;
}


//初始化客户归属下拉列表
function ascription() {
    var user_data = {}
    var succCallBack = function (data, status, response) {
        if (data.errorCode == "0") {
            var dataInter = data.group;
            var interText = doT.template($("#ascription_bob").text());
            $("#ascription_lsit").html(interText(dataInter));
        }
    };
    return web.doAjax(root.inter.getGroup, 'post', user_data, succCallBack);
}

function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}